﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Data;

namespace Carriage
{
    public class AramexClass
    {
        private string path = System.Web.HttpContext.Current.Server.MapPath("~/AramexPrice.xml");

        private double _weight = 0;
        /// <summary>
        /// 货物净重(单位:克) 
        /// </summary>
        internal double weight
        {
            get
            {
                return _weight;
            }
            set
            {
                _weight = value;
            }
        }

        private double _weightrate = 1.3;
        /// <summary>
        /// 毛重与净重的比率
        /// </summary>
        public double weightrate
        {
            get
            {
                return _weightrate;
            }
            set
            {
                _weightrate = value;
            }
        }

        private double _Fuelcosts = 1.17;
        /// <summary>
        /// 燃油费系数
        /// </summary>
        public double Fuelcosts
        {
            get
            {
                return _Fuelcosts;
            }
            set
            {
                _Fuelcosts = value;
            }
        }

        private double _Surcharge = 0;
        /// <summary>
        /// 附加费
        /// </summary>
        public double Surcharge
        {
            get
            {
                return _Surcharge;
            }
            set
            {
                _Surcharge = value;
            }
        }

        private double value1 = 0;//起重标准
        private double value2 = 0;//续重标准
        private double value3 = 1;//折扣

        /// <summary>
        /// 获取运费价格
        /// </summary>
        private void SetPrices(string country)
        {
            string filePath = System.Web.HttpContext.Current.Server.MapPath("~/AramexPrice.xml");
            System.IO.FileInfo fi = new System.IO.FileInfo(filePath);

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(filePath);
            foreach (System.Xml.XmlNode node in doc.SelectNodes("Root//Country"))
            {
                if (country.ToLower() == node.SelectSingleNode("Name").InnerText.ToLower())
                {
                    //获取首重和续重 以及计费方式
                    value1 = double.Parse(node.SelectSingleNode("Begincost").InnerText);
                    value2 = double.Parse(node.SelectSingleNode("Extracost").InnerText);
                    value3 = double.Parse(node.SelectSingleNode("Discount").InnerText);

                    return;
                }
            }
            doc = null;
        }

        /// <summary>
        /// 获取Aramex的运费
        /// 所需条件:
        /// １.目的国家所在ｄｈｌ分布区区号
        /// ２.重量
        /// ３.读取所在区的价格信息
        /// </summary>
        /// <returns></returns>
        internal double GetCarriage(string country)
        {
            value1 = value2 = 0;
            value3 = 1;

            double rlt = 0;

            double cw = (double)(this.weight * this.weightrate);//货物的毛重

            //cw = double.Round(cw,2);
            double cwKg = cw / 1000;
            //cwKg = double.Round(cwKg,2);

            SetPrices(country);

            if (value1 != 0)
            {
                //按0.5kg计算
                if (cw <= 500)
                    rlt = value1;
                else
                    rlt = value1 + ((cw - 500) / 500) * value2;


                return rlt * value3 * _Fuelcosts + _Surcharge;//标准运费乘折扣

            }
            else
                return 0;
        }
    }
}
